package lab4Source;

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Iterator;
import java.util.Vector;

public class S_PaintVisitor extends AbstractVisitor {	
	private java.awt.Graphics graphics;
	
	public void S_setGraphics(java.awt.Graphics graphics) {
		this.graphics = graphics;
	}
	
	@Override
	public void visit(Square s) {
		this.graphics.setColor(new Color((200 - s.getDepth()*20), (200 - s.getDepth()*20), (200 - s.getDepth()*20)));
		this.graphics.fillRect(s.getX(), s.getY(), s.getWidth(), s.getHeight());
		Vector children = s.S_getChildren();
		if (!children.isEmpty()) {
			this.S_paintSquaresChildren(children);
		}
	}
	
	public void S_paintSquaresChildren(Vector children) {
		AbstractShape s;
		Iterator iter = children.iterator();
		while (iter.hasNext()) {
			s = (AbstractShape)iter.next();
			s.paint(this.graphics);
		}
	}

	@Override
	public void visit(Rectangle r) {
		this.graphics.setColor(Color.blue);
		this.graphics.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
	}

	@Override
	public void visit(Circle c) {		
		this.graphics.setColor(Color.red);
		this.graphics.fillOval(c.getX(), c.getY(), c.getWidth(), c.getHeight());
	}

	@Override
	public void visit(Triangle t) {
		Graphics2D g2 = (Graphics2D)this.graphics;
		if (null == t.S_getPolygon()) {
			t.S_createPolygon(); 			
			t.S_getPolygon().addPoint(t.getX() + t.getWidth() / 2, t.getY());
			t.S_getPolygon().addPoint(t.getX(), t.getY() + t.getHeight());
			t.S_getPolygon().addPoint(t.getX() + t.getWidth(), t.getY() + t.getHeight());
		}
		g2.setColor(Color.green);
		g2.fill(t.S_getPolygon());
	}
	
	@Override
	public void visit(SquareProxy sp) {}
}
